// pages/accidentReport/reportCreate/index.js
import urls from '../../../requestUrl.js'
import request from '../../../baseRequest.js'
const {
    $Message
} = require('../../../components/base/index.js');


Page({

    /**
     * 页面的初始数据
     */
    data: {
        insuranceInfo: {},
        uploaderInfo: {
            title: '事故现场照片',
            files: [],
            count: 20,
            uploadUrl: urls.baseUrl + urls.uploadUrl.commons,
            isUpload: false,
            sourceType: 'camera', //'all/camera'
            attentionText: '1.请按要求上传事故现场的真实图片\n2.上传不少于1张图片,图片必须拍照上传',
            uploadComplete: 'uploadCompleteListence'
        },
        modalActions: [{
                name: '放弃上报'
            },
            {
                name: '立即授权',
                openType: 'openSetting',
                color: '#2d8cf0',
            },
        ],
        showModal: false
    },
    /**监听图片上传完成情况*/
    uploadCompleteListence(e) {
        console.log(e)
        let detail = e.detail
        if (detail.length === this.data.formData.accidentImgs.length) {
            this.data.formData.accidentImgs = detail
            console.log(this.data.formData)
            this.handelCreateAccidentOrder()
        } else {
            this.setData({
                showSpin: false
            })
            this.showErrorMessage('图片上传有误')
        }
    },
    /**处理表单提交事件 */
    handleFormSubmit(e) {
        if (this.checkForForm(e.detail.value)) {
            this.data.formData = e.detail.value
            this.setData({
                    showSpin: true,
                    ['uploaderInfo.isUpload']: true
                })
                // 返回顶部
            wx.pageScrollTo({
                scrollTop: 0,
            })
        }
    },
    /**表单校验 */
    checkForForm(data) {
        if (data.accidentImgs.length === 0) {
            this.showErrorMessage('请上传事故现场照片')
            return false
        } else if (data.accidentImgs.length < 1) {
            this.showErrorMessage('请上传事故现场照片不少于1张')
            return false
        }
        if (!data.accidentDescription) {
            this.showErrorMessage('请填写事故现场描述')
            return false
        }
        return true
    },
    /**显示错误提示 */
    showErrorMessage(str) {
        $Message({
            content: str,
            type: 'error'
        });
    },
    /**获取服务单基本信息 */
    getInsuranceBaseInfo() {
        /**
         *
         */
        let url = `${urls.accident.getOrderInfo}/${this.data.orderId}`
        request.getRequest(url, {}, (res) => {
            this.setData({
                insuranceInfo: res.insuranceInfo
            })
        }, () => {})

        // let res = {
        //   insuranceInfo: {
        //     orderNo: '201810091092',
        //     serviceType: '载物意外责任服务产品1',
        //     buyerName: '张三三',
        //     buyerTelphone: '13431526712'
        //   }
        // }
        // this.setData({
        //   insuranceInfo: res.insuranceInfo
        // })
    },
    /**创建事故订单 */
    handelCreateAccidentOrder() {
        let _accidentInfo = this.data.formData
        _accidentInfo.accidentLocation = this.data.accidentLocation
        let param = {
            orderId: this.data.orderId,
            openid: this.data.openid,
            accidentInfo: _accidentInfo
        }

        request.postRequest(urls.accident.accidentCreate, param, (res) => {
            this.setData({
                showSpin: false
            })
            let id = res.id
            wx.showToast({
                title: '创建成功',
                complete: () => {
                    wx.redirectTo({
                        url: `/pages/accidentReport/reportDetail/index?id=${id}`
                    })
                }
            })
        }, () => {})
    },
    /**
     * 处理弹出框的按钮点击事件
     * @param {*} e
     */
    handleModalActionClick(e) {
        let _index = e.detail.index
        switch (_index) {
            case 0:
                wx.navigateBack()
                break;
            case 1:
                this.setData({
                    showModal: false
                })
                break;
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.data.orderId = options.orderid
        this.data.openid = getApp().getUserOpenid()
        this.getInsuranceBaseInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        wx.getLocation({
            type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
            success: (res) => {
                // success
                this.data.accidentLocation = { 'latitude': res.latitude, 'longitude': res.longitude }
                console.log(res, this.data.accidentLocation)
            },
            fail: (res) => {
                // fail
                this.setData({
                    showModal: true
                })
            },
            complete: function() {
                // complete
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage: function() {

    // }
})
